<?php

namespace app\index\controller;

use model\InsurantModel;
use model\PolicyModel;
use model\UserInfoModel;
use think\Db;
use think\Exception;
use think\Request;
use library\SmsDemo;
/**
 * Desc: 被保险人页面
 * Class Insurant
 * Created By Doyi
 * Date: 2018/10/29
 */
class Insurant extends CommonController
{
    const JOIN_PRICE = 1;               // 参与竞价
    const LOOK_GOODS_INFO = 2;          // 看货信息
    const BOL_INFO = 3;                 // 提货信息
    const RECEIVE_MONEY = 4;            // 收款信息
    const AUTH_REPORT = 5;              // 递交授权书

    const NO_COMMIT = 1;                // 未提交
    const NO_CHECK = 2;                 // 待审核
    const RETURN_CHANGE = 3;            // 驳回修改
    const READY = 4;                    // 已完成

    public $request;

    /**
     * 根据模块分配字段
     * @var array
     */
    public $modules = [
        self::JOIN_PRICE =>[
            'is_join',
            'is_promise',
            'back_price',
        ],
        self::LOOK_GOODS_INFO =>[
            'look_type',
            'address',
            'address_detail',
            'linkman',
            'phone',
            'second_phone',
        ],
        self::BOL_INFO =>[
            'is_pay',
            'pay_result',
            'bol_info',
            'bol_address',
            'bol_address_detail',
            'bol_linkman',
            'bol_phone',
            'bol_days',
        ],
        self::RECEIVE_MONEY =>[
            'pay_name',
            'bank_name',
            'card_num',
            'tax_num',
            'company_address',
        ],
        self::AUTH_REPORT =>[
            'entrust_url',
        ],
    ];


    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->request = $request->param();
    }

    public function getMenu()
    {
        $param = $this->request->param();
    }

    /**
     * 被保险人--核对信息
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCheckInfo()
    {
        $where = [
            'insurant.delete_ts' => 0,
            'insurant.status' => 0,
            'policy.policy_status' => 5,
            'policy.remer_id' => $this->uid,
        ];
        if(isset($this->request['report_num']) && !empty($this->request['report_num'])){
            $where['policy.report_num'] = $this->request['report_num'];
        }

        $res = Db::table('insurant')->where($where)->join('policy','policy.p_id=insurant.p_id','left'
        )->field('insurant.i_id,policy.created_at,insurant.status,policy.policy_num')->select();
        return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 被保险人-- 资料未确认
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getGoodInfo()
    {
        $policy_num = $this->request['policy_num'];
        $PolicyModel = new PolicyModel();
        $info = $PolicyModel->getPolicyInfo($policy_num)->toArray();
//        $info['insurance_type_name'] = $this->getInsurantType($info['insurance_type']);
//        $info['goods_type_name'] = $this->getGoodType($info['goods_type']);
        return $this->response->json(['data'=>$info,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 被保险人--下一步--获取信息
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getNextBol()
    {
        $p_id = isset($this->request['p_id']) ? $this->request['p_id'] : '';
        $where = [
            'p_id'=>$p_id,
        ];
        $res = Db::table('insurant')->where($where)->update(['status'=>1]);
        if($res){
            return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }else{
            return $this->response->json(['data'=>[],'code'=>self::SUCCESS_CODE,'msg'=>'fail']);
        }
    }




    /**
     * 被保险人---下一步--保存
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function saveBolInfo()
    {
        $id = isset($this->request['i_id']) ? $this->request['i_id'] : '';
        if(!empty($this->request['save_price'])){
            $data['save_price'] = $this->request['save_price'];
        }
        if(!empty($this->request['set_price'])){
            $data['set_price'] = $this->request['set_price'];
        }
        if(!empty($this->request['is_join'])){
            $data['is_join'] = $this->request['is_join'];
        }
        if(!empty($this->request['is_promise'])){
            $data['is_promise'] = $this->request['is_promise'];
        }
        if(!empty($this->request['back_price'])){
            $data['back_price'] = $this->request['back_price'];
        }

        if(!empty($this->request['province'])){
            $data['province'] = $this->request['province'];
        }
        if(!empty($this->request['city'])){
            $data['city'] = $this->request['city'];
        }
        if(!empty($this->request['town'])){
            $data['town'] = $this->request['town'];
        }

        if(!empty($this->request['address_detail'])){
            $data['address_detail'] = $this->request['address_detail'];
        }
        if(!empty($this->request['linkman'])){
            $data['linkman'] = $this->request['linkman'];
        }
        if(!empty($this->request['phone'])){
            $data['phone'] = $this->request['phone'];
        }
        if(!empty($this->request['second_phone'])){
            $data['second_phone'] = $this->request['second_phone'];
        }
        if(!empty($this->request['is_pay'])){
            $data['is_pay'] = $this->request['is_pay'];
        }
        if(!empty($this->request['pay_result'])){
            $data['pay_result'] = $this->request['pay_result'];
        }
        if(!empty($this->request['bol_info'])){
            $data['bol_info'] = $this->request['bol_info'];
        }
        if(!empty($this->request['bol_province'])){
            $data['bol_province'] = $this->request['bol_province'];
        }
        if(!empty($this->request['bol_city'])){
            $data['bol_city'] = $this->request['bol_city'];
        }
        if(!empty($this->request['bol_town'])){
            $data['bol_town'] = $this->request['bol_town'];
        }
        if(!empty($this->request['bol_address_detail'])){
            $data['bol_address_detail'] = $this->request['bol_address_detail'];
        }
        if(!empty($this->request['bol_linkman'])){
            $data['bol_linkman'] = $this->request['bol_linkman'];
        }
        if(!empty($this->request['bol_phone'])){
            $data['bol_phone'] = $this->request['bol_phone'];
        }
        if(!empty($this->request['pay_name'])){
            $data['pay_name'] = $this->request['pay_name'];
        }
        if(!empty($this->request['card_num'])){
            $data['card_num'] = $this->request['card_num'];
        }
        if(!empty($this->request['tax_num'])){
            $data['tax_num'] = $this->request['tax_num'];
        }
        if(!empty($this->request['company_address'])){
            $data['company_address'] = $this->request['company_address'];
        }
        if(!empty($this->request['entrust_url'])){
            $data['entrust_url'] = $this->request['entrust_url'];
        }
        if(!empty($this->request['bol_url'])){
            $data['bol_url'] = $this->request['bol_url'];
        }
        if(!empty($this->request['bol_days'])){
            $data['bol_days'] = $this->request['bol_days'];
        }
        if(!empty($this->request['look_type'])){
            $data['look_type'] = $this->request['look_type'];
        }
        if(!empty($this->request['now_status'])){
            $data['now_status'] = $this->request['now_status'];
        }
        if($data['now_status'] == 5){
            $data['status'] = 2;
        }
        $data['update_at'] = date('Y-m-d H:i:s',time());
        $InsurantModel = new InsurantModel();
        $res = $InsurantModel->where('i_id',$id)->update($data);
        if($res){
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }else{
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'fail']);
        }
    }




    /**
     * 获取历史列表
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHistoryInfo()
    {
        $where['insurant.ui_id'] = $this->uid;
        $where['insurant.status'] = ['not in',0];
        $status = isset($this->request['status']) ? $this->request['status'] : 5;
        if($status != 5){
            $where['insurant.status'] = $status;
        }
        $res = Db::table('insurant')->join('policy p','p.p_id=insurant.p_id','LEFT')->field('insurant.i_id,p.report_num,insurant.status,insurant.created_at')->select();
        return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 历史详情（所有状态）
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function historyInfoById()
    {
        $i_id = $this->request['i_id'];
        $data['res'] = Db::table('insurant')->where('i_id',$i_id)->find();
        if($data['res']['status'] == 3){
            $where = [
                'i_id'=>$i_id,
                'status'=>1,
            ];
            $returnMsg = Db::table('return_insurant')->where($where)->select();
            $a =[];
            foreach($returnMsg as $k=>$v){
                    $a['join_price'][] = $v['join_price'];
                    $a['look_goods_info'][] = $v['look_goods_info'];
                    $a['bol_info'][] = $v['bol_info'];
                    $a['receive_money'][] = $v['receive_money'];
                    $a['auth_report'][] = $v['auth_report'];
            }
            $data['return_msg'] = $a;
        }
        return $this->response->json(['data'=>$data,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }


    /**
     * 打印上传列表(3个通用)
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function download_list()
    {
        $ui_id = $this->uid;
        $type = $this->request['type'];
        $res = Db::table('policy')->where('remer_id',$ui_id)->field('p_id,policy_num,created_at')->select();
        foreach ($res as $k=>$v){
            $where = [
                'type'=>$type,
                'policy_num'=>$v['policy_num']
            ];
            $info = Db::table('download_info')->where($where)->find();
            if($info){
                $res[$k]['is_show'] = 1;
            }else{
                $res[$k]['is_show'] = 0;
            }
        }
        return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }


    /**
     * 打印委托书
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function print_report()
    {
        $policy_num = $this->request['policy_num'];
        $res = Db::table('policy')->where('policy_num',$policy_num)->field('p_id,policy_num')->find();
        $info = Db::table('insurant')->where('p_id',$res['p_id'])->find();
        $res['back_price'] = $info['back_price'];
        return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 提货单下载 （查看）
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function download_bol()
    {
        $policy_num = $this->request['policy_num'];
        $res = Db::table('policy')
            ->where('policy_num',$policy_num)
            ->join('insurant','insurant.p_id=policy.p_id','left')
            ->field('insurant.bol_url')
            ->find();
        return $this->response->json(['data'=>$res,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }

    /**
     * 上传提货单
     * @return \think\Response
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function upload_bol()
    {
        $policy_num = $this->request['policy_num'];
        $res = Db::table('policy')->where('policy_num',$policy_num)->field('p_id,policy_num')->find();
        $info = Db::table('insurant')->where('p_id',$res['p_id'])->update(['bol_url'=>$this->request['bol_url']]);
        if($info){
            return $this->response->json(['data'=>'','code'=>self::SUCCESS_CODE,'msg'=>'success']);
        }else{
            return $this->response->json(['data'=>'','code'=>self::ERROR_CODE,'msg'=>'fail']);
        }
    }



    /**
     * 获取看货要求
     * @return \think\Response
     */
    public function getLookType()
    {
        $data = Db::table('look_type')->select();
        return $this->response->json(['data'=>$data,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }


    /**
     * 返回提货时限
     * @return \think\response\Json
     */
    public function getBolDays()
    {
        $data = Db::table('bol_days')->select();
        return $this->response->json(['data'=>$data,'code'=>self::SUCCESS_CODE,'msg'=>'success']);
    }
}